﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Profile;
using COFE.Core;

namespace COFE.Web
{
    public class WSUserProfileManager : IUserProfileManager2
    {
        #region Constructor

        public WSUserProfileManager(IUserProfileManager2 orgUserProfileManager)
        {
            _orgUserProfileManager = orgUserProfileManager;
        }

        #endregion

        #region Methods

        public void SetCurrentUserProfile(IUserProfile userProfile)
        {
            _orgUserProfileManager.SetCurrentUserProfile(userProfile);

            lock (_userServiceProfilesLock)
            {
                if (!_userServiceProfiles.ContainsKey(userProfile.UserName))                    
                    _userServiceProfiles.Add(userProfile.UserName, new UserServiceProfile(userProfile as IUserProfile2));                
                CurrentUserServiceProfile = _userServiceProfiles[userProfile.UserName];
            }

            CurrentUserProfileChanged(this, EventArgs.Empty);
        }

        ///// <summary>
        ///// Construct a web user manager using IUserDBProfile and add it to the manager list.
        ///// </summary>
        ///// <param name="userProfile"></param>
        ///// <returns></returns>
        //public static IUserServiceProfile Add(IUserProfile userProfile)
        //{
        //    if (!_userServiceProfiles.ContainsKey(userProfile.UserName))
        //        _userServiceProfiles.Add(userProfile.UserName, new UserServiceProfile(userProfile));
        //    return _userServiceProfiles[userProfile.UserName];
        //}

        /// <summary>
        /// Lookup a web user manager.
        /// </summary>
        /// <param name="lookupFunc"></param>
        /// <returns></returns>
        public static IUserServiceProfile Find(Func<IUserServiceProfile, bool> lookupFunc)
        {
            foreach (var usp in _userServiceProfiles.Values)
                if (lookupFunc(usp))
                    return usp;
            return null;
        }

        public IUserProfile2 CreateUser(string userName, string password)
        {
            return _orgUserProfileManager.CreateUser(userName, password);
        }

        public IUserProfile2 GetUserProfile(string userName, string password)
        {
            return _orgUserProfileManager.GetUserProfile(userName, password);
        }

        public void DeleteUser(string userName)
        {
            _orgUserProfileManager.DeleteUser(userName);
        }

        public IUserProfile2 GetUserProfile(Guid authorizationKey)
        {
            return _orgUserProfileManager.GetUserProfile(authorizationKey);
        }

        public void Logout(Guid authorizationKey)
        {
            _orgUserProfileManager.Logout(authorizationKey);
        }

        #endregion

        #region Data

        IUserProfileManager2 _orgUserProfileManager;

        private static object _userServiceProfilesLock = new object();
        private static Dictionary<string, IUserServiceProfile> _userServiceProfiles
         = new Dictionary<string, IUserServiceProfile>();

        #endregion

        #region Public Properties

        public event EventHandler CurrentUserProfileChanged = (o,e) => { };

        public IUserProfile AdminUserProfile { get { return _orgUserProfileManager.AdminUserProfile; } }

        public IUserProfile CurrentUserProfile { get { return _orgUserProfileManager.CurrentUserProfile; } }

        public IUserServiceProfile CurrentUserServiceProfile { get; private set; }


        public IEnumerable<IUserProfile2> Users
        {
            get { return _orgUserProfileManager.Users; }
        }

        #endregion












    }
}
